Skip to content

Add suppress warnings & remove redudant cast quick-fix code action - #8646

Open
zielinsky wants to merge 13 commits into
scalameta:main-v2from
zielinsky:metalsv2/supress-warnings
Open

Add suppress warnings & remove redudant cast quick-fix code action#8646
zielinsky wants to merge 13 commits into
scalameta:main-v2from
zielinsky:metalsv2/supress-warnings

Conversation

@zielinsky

@zielinsky zielinsky commented Jul 6, 2026

Copy link
Copy Markdown
Member

Part of #8502

Summary by CodeRabbit

  • New Features

    • Added a Java quick fix to remove redundant casts.
    • Added a Java quick fix to apply or update @SuppressWarnings annotations for compiler warnings.
    • Improved warning diagnostics so available quick fixes can be identified more accurately.
  • Bug Fixes

    • Preserved relevant Java lint options when custom Java compiler options are disabled.
    • Improved Java target selection for projects containing Java sources.
    • Enhanced handling of annotations, casts, whitespace, and diagnostic ranges in Java code actions.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • No new commits to review - use @coderabbitai full review for a full pass

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: eb160a42-61f9-42a8-8b2e-328f4c2242c1

📥 Commits

Reviewing files that changed from the base of the PR and between 67465e5 and 6d545b7.

📒 Files selected for processing (1)
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
🚧 Files skipped from review as they are similar to previous changes (1)
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala

📝 Walkthrough

Walkthrough

Adds Java quick fixes for redundant casts and warning suppression. Extends Java tree parsing and diagnostic metadata. Updates Java compiler options, JVM target selection, action registration, and LSP test infrastructure.

Changes

Java compiler integration

Layer / File(s) Summary
Java compiler options and target selection
metals/src/main/scala/scala/meta/internal/metals/{CompilerConfiguration,BuildTargets,TargetData,Compilers,ModuleStatus}.scala, metals/src/main/scala/scala/meta/internal/metals/{debug/server/DebugeeParamsCreator,mcp/MetalsMcpTools}.scala
Java compiler options retain lint settings when JAVAC_OPTIONS is disabled. JVM target lookup can prefer Java targets. Related target mappings use explicit functions.

Java source analysis

Layer / File(s) Summary
Java tree and diagnostic support
metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala, mtags-java/src/main/scala/scala/meta/internal/jpc/JavaDiagnostics.scala
JavaTrees locates casts and annotations and records variable ownership. Javac lint categories are attached to LSP diagnostic data.

Redundant cast quick fix

Layer / File(s) Summary
Redundant cast quick fix and coverage
metals/src/main/scala/scala/meta/internal/metals/codeactions/RemoveRedundantCast.scala, tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala
Adds a Java action that removes redundant casts while handling whitespace and line breaks. LSP tests cover expressions, arrays, primitives, method calls, initializers, and lambdas.

Warning suppression quick fix

Layer / File(s) Summary
Suppress-warnings quick fix and annotation updates
metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
Adds Java warning suppression actions. The implementation targets declarations and inserts or updates @SuppressWarnings annotations for scalar, named, and array-valued arguments.

Action wiring and validation

Layer / File(s) Summary
Code-action registration and LSP validation
metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala, tests/unit/src/main/scala/tests/{MbtJsonBuilder.scala,codeactions/BaseCodeActionLspSuite.scala}, tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
Registers both Java actions, passes javacOptions through MBT layouts, and tests suppression actions across Java warning categories and annotation forms.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant LSPClient
  participant CodeActionProvider
  participant RemoveRedundantCast
  participant SuppressWarnings
  participant JavaTrees
  LSPClient->>CodeActionProvider: request Java diagnostic actions
  CodeActionProvider->>RemoveRedundantCast: process redundant-cast diagnostic
  RemoveRedundantCast->>JavaTrees: locate type cast
  JavaTrees-->>RemoveRedundantCast: return cast ranges
  RemoveRedundantCast-->>CodeActionProvider: return cast-removal edit
  CodeActionProvider->>SuppressWarnings: process javac warning
  SuppressWarnings->>JavaTrees: locate declaration and annotations
  JavaTrees-->>SuppressWarnings: return source ranges
  SuppressWarnings-->>CodeActionProvider: return suppression edit
  CodeActionProvider-->>LSPClient: return code actions
Loading

Possibly related PRs

Suggested reviewers: tgodzik

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the two primary changes: adding suppress-warnings and redundant-cast quick-fix code actions.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch 3 times, most recently from 0d30058 to 3af8b1e Compare July 6, 2026 13:51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala (1)

436-452: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Naming ambiguity: field javaTarget is typed JvmTarget, then pattern-matched against JavaTarget.

This isn't introduced by this diff, but the changed lines make the ambiguity more visible: javaTarget: JvmTarget (line 437) is matched with case j: JavaTarget => j.options (line 450). Consider renaming the field (e.g. target: JvmTarget) to reduce confusion now that the surrounding PR stack explicitly distinguishes JavaTarget from JvmTarget.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala`
around lines 436 - 452, The `JavaLazyCompiler` field name is ambiguous because
`javaTarget` is typed as `JvmTarget` but later pattern-matched against
`JavaTarget` in `newCompiler`. Rename the field to something neutral like
`target` (and update its uses such as `buildTargetId` and the `javaTarget match`
block) so the `JavaTarget` vs `JvmTarget` distinction is clear and the code is
easier to read.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala`:
- Around line 91-112: Update warningName in SuppressWarnings so it only returns
a suppressible category for javac warnings, not ERROR diagnostics; gate the
lookup on diagnostic severity before calling warningNameFrom. Also tighten
warningNameFrom to avoid substring matches by using word-boundary/whole-token
matching instead of plain text.contains, and ensure overlapping lint names are
resolved deterministically so cases like unchecked cast map to the correct
category.

---

Nitpick comments:
In
`@metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala`:
- Around line 436-452: The `JavaLazyCompiler` field name is ambiguous because
`javaTarget` is typed as `JvmTarget` but later pattern-matched against
`JavaTarget` in `newCompiler`. Rename the field to something neutral like
`target` (and update its uses such as `buildTargetId` and the `javaTarget match`
block) so the `JavaTarget` vs `JvmTarget` distinction is clear and the code is
easier to read.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 64c263cf-77b1-49f1-a005-7a55602498a2

📥 Commits

Reviewing files that changed from the base of the PR and between e862680 and 0d30058.

📒 Files selected for processing (7)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • mtags-interfaces/src/main/java/scala/meta/infra/FeatureFlag.java
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
💤 Files with no reviewable changes (1)
  • mtags-interfaces/src/main/java/scala/meta/infra/FeatureFlag.java

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from 3af8b1e to b217fe0 Compare July 6, 2026 16:18

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unit/src/test/scala/tests/InfraSuite.scala (1)

15-22: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Avoid leaking enabledFlags between suites. SuppressWarningsLspSuite.beforeAll() mutates a shared singleton before calling super.beforeAll(), so a setup failure can leave JAVAC_OPTIONS enabled for later suites. Move the toggle into a fixture/try-finally teardown or reset the set centrally.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/src/test/scala/tests/InfraSuite.scala` around lines 15 - 22, The
shared TestingInfra.enabledFlags set is being mutated by
SuppressWarningsLspSuite.beforeAll() without guaranteed cleanup, so a failed
setup can leak state into later suites. Update the test setup to either use a
fixture or wrap the toggle in a try-finally teardown that always removes the
JAVAC_OPTIONS flag, or centralize reset logic for TestingInfra.enabledFlags so
each suite starts from a clean state. Use the existing TestingInfra singleton
and SuppressWarningsLspSuite.beforeAll() as the main places to apply the fix.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@tests/unit/src/test/scala/tests/InfraSuite.scala`:
- Around line 15-22: The shared TestingInfra.enabledFlags set is being mutated
by SuppressWarningsLspSuite.beforeAll() without guaranteed cleanup, so a failed
setup can leak state into later suites. Update the test setup to either use a
fixture or wrap the toggle in a try-finally teardown that always removes the
JAVAC_OPTIONS flag, or centralize reset logic for TestingInfra.enabledFlags so
each suite starts from a clean state. Use the existing TestingInfra singleton
and SuppressWarningsLspSuite.beforeAll() as the main places to apply the fix.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e08126e6-9956-4f39-94cd-b85abb70e10d

📥 Commits

Reviewing files that changed from the base of the PR and between 3af8b1e and b217fe0.

📒 Files selected for processing (7)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
🚧 Files skipped from review as they are similar to previous changes (6)
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch 4 times, most recently from 5e375ac to 249d4f9 Compare July 9, 2026 10:19

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala (1)

802-812: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider documenting the concurrent diagnostics-wait pattern.

The diagnosticsPublished = server.awaitNextDiagnostics(...) assignment before _ <- server.didFocus(path) is a deliberate pattern to register the diagnostics handler before the focus event triggers publishing. A brief comment would help future readers understand why the future is created here rather than inline with <-.

📝 Suggested comment
       _ <- server.didOpen(path)
+      // Register the diagnostics handler before didFocus so diagnostics
+      // triggered by the focus event are captured.
       diagnosticsPublished =
         server.awaitNextDiagnostics(
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`
around lines 802 - 812, The diagnostics wait is intentionally started before the
focus event to avoid missing the publish, so document this concurrent pattern in
the test flow around the diagnosticsPublished assignment and server.didFocus
call. Add a brief comment near the awaitNextDiagnostics setup in
SuppressWarningsLspSuite to explain that the handler must be registered first,
then the focus event can trigger diagnostics, and keep the existing sequencing
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`:
- Around line 802-812: The diagnostics wait is intentionally started before the
focus event to avoid missing the publish, so document this concurrent pattern in
the test flow around the diagnosticsPublished assignment and server.didFocus
call. Add a brief comment near the awaitNextDiagnostics setup in
SuppressWarningsLspSuite to explain that the handler must be registered first,
then the focus event can trigger diagnostics, and keep the existing sequencing
unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 67e22879-e85c-4937-a673-59bcb6707da5

📥 Commits

Reviewing files that changed from the base of the PR and between b217fe0 and 249d4f9.

📒 Files selected for processing (9)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
🚧 Files skipped from review as they are similar to previous changes (6)
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from 249d4f9 to f79fc10 Compare July 9, 2026 13:52
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from c9a3884 to e6255ce Compare July 9, 2026 22:34
@zielinsky zielinsky changed the title Add suppress warnings quick-fix code action Add suppress warnings & remove redudant cast quick-fix code action Jul 9, 2026
@zielinsky
zielinsky marked this pull request as ready for review August 6, 2026 20:43
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala (1)

621-719: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Add a runtime-JDK guard for JDK-only lint cases.

These fixtures require newer compiler diagnostics: this-escape needs JDK 21, value-based class synchronization needs JDK 16, and obsolete strictfp needs JDK 17. On older CI runtimes, checkSuppressWarnings waits for diagnostics that do not exist. Guard these cases with a Java-version assume, or split the suite by JDK versions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`
around lines 621 - 719, Add Java runtime-version assumptions to the
JDK-dependent cases in checkSuppressWarnings: require JDK 17+ for
strictfp-method, JDK 16+ for synchronization-method, and JDK 21+ for
this-escape-constructor. Keep the existing fixtures and assertions unchanged,
and leave the text-blocks-method case unguarded unless its diagnostics also
require a version check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala`:
- Line 559: Update the Java lint options default formatting near
JavaLintOptions.default so each option value is quoted before joining, producing
a valid JSON array of strings such as ["cast","deprecation",...].
- Around line 50-60: Remove "this-escape" from the allValues used to construct
JavaLintOptions.default, while preserving it in allowed if user configuration
should still accept it. Ensure the default lint flags remain compatible with
Java 11/17.

---

Nitpick comments:
In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`:
- Around line 621-719: Add Java runtime-version assumptions to the JDK-dependent
cases in checkSuppressWarnings: require JDK 17+ for strictfp-method, JDK 16+ for
synchronization-method, and JDK 21+ for this-escape-constructor. Keep the
existing fixtures and assertions unchanged, and leave the text-blocks-method
case unguarded unless its diagnostics also require a version check.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 42ea884d-32e3-4622-a2f0-2f9c7d541875

📥 Commits

Reviewing files that changed from the base of the PR and between 79568e2 and d263f9f.

📒 Files selected for processing (12)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/RemoveRedundantCast.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • tests/unit/src/main/scala/tests/BaseLspSuite.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
🚧 Files skipped from review as they are similar to previous changes (6)
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala

Comment thread metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala Outdated
Comment thread metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala Outdated
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch 3 times, most recently from 431f438 to f437208 Compare August 6, 2026 22:08
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from f437208 to 301267b Compare August 6, 2026 22:15
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala (1)

31-32: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the l alias for CodeAction.

Line 8 imports org.eclipse.{lsp4j => l}. Line 31 uses the fully qualified org.eclipse.lsp4j.CodeAction instead. If no other reference to l exists in this file, the alias import becomes unused. The Metals build enables fatal warnings for unused imports in several modules, so this can break compilation.

♻️ Proposed change
-  private val onlyRemoveRedundantCast: org.eclipse.lsp4j.CodeAction => Boolean =
+  private val onlyRemoveRedundantCast: l.CodeAction => Boolean =
     _.getTitle() == RemoveRedundantCast.title

Compile this module with Metals MCP tools to confirm no unused-import warning is raised.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala`
around lines 31 - 32, Update the onlyRemoveRedundantCast type annotation to use
the imported l alias instead of the fully qualified org.eclipse.lsp4j name,
ensuring the existing lsp4j alias import remains used and the CodeAction
predicate behavior is unchanged.

Source: Coding guidelines

tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala (1)

82-89: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Extract the repeated DeprecatedApi layout into a shared value.

The same DeprecatedApi.java layout appears three times. Define it once as a private val and reuse it.

♻️ Proposed refactor
+  private val deprecatedApiLayout =
+    """|/a/src/main/java/a/DeprecatedApi.java
+       |package a;
+       |
+       |class DeprecatedApi {
+       |  `@Deprecated`
+       |  static void old() {}
+       |}
+       |""".stripMargin

Then pass extraLayout = deprecatedApiLayout in the three cases.

Also applies to: 218-225, 281-288

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`
around lines 82 - 89, Extract the repeated DeprecatedApi.java content into a
private shared value in SuppressWarningsLspSuite, then replace each of the three
inline layouts with extraLayout = deprecatedApiLayout. Preserve the existing
string contents and stripMargin behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala`:
- Around line 229-260: Update appendWarningEdit to handle empty annotation
arrays and named value forms before constructing the TextEdit: render an empty
{} as {"warning"}, and preserve value = syntax by converting scalar or array
named values into valid forms such as {value = "deprecation", "warning"}. Keep
the existing duplicate-warning check and add coverage for empty, scalar named,
and array named annotations.

---

Nitpick comments:
In
`@tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala`:
- Around line 31-32: Update the onlyRemoveRedundantCast type annotation to use
the imported l alias instead of the fully qualified org.eclipse.lsp4j name,
ensuring the existing lsp4j alias import remains used and the CodeAction
predicate behavior is unchanged.

In `@tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala`:
- Around line 82-89: Extract the repeated DeprecatedApi.java content into a
private shared value in SuppressWarningsLspSuite, then replace each of the three
inline layouts with extraLayout = deprecatedApiLayout. Preserve the existing
string contents and stripMargin behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ac5d9c3a-21f5-4897-b0bb-9e475fbc3e81

📥 Commits

Reviewing files that changed from the base of the PR and between 79568e2 and 301267b.

📒 Files selected for processing (13)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/RemoveRedundantCast.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • tests/unit/src/main/scala/tests/BaseLspSuite.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
🚧 Files skipped from review as they are similar to previous changes (7)
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/InfraSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
  • tests/unit/src/main/scala/tests/BaseLspSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch 2 times, most recently from 03d2b80 to 7d98a0b Compare August 6, 2026 23:06
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from 7d98a0b to b5dcdaf Compare August 9, 2026 21:00
@zielinsky
zielinsky requested a review from tgodzik August 10, 2026 09:35
if (includeAll) options
else
options.filter(option =>
option == "-Xlint" || option.startsWith("-Xlint:")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to filter -Xlint options?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing javac options to the javapc was behind a feature flag, but lint options seem useful to always include while preserving the previous behavior.

Comment thread metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala Outdated
Comment thread tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala Outdated
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from a0bbffb to 6393911 Compare August 11, 2026 10:40

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala (2)

54-54: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Do not deduplicate actions only by title.

If a request range contains diagnostics of the same lint category on different declarations, Line 54 builds different edits and then drops all but the first action because their titles match. Deduplicate by target or edit identity instead. Add coverage with two declarations that produce the same warning category.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala`
at line 54, Update the action deduplication in SuppressWarnings so actions with
the same title but different target declarations or edits are preserved;
deduplicate using target or edit identity instead of getTitle(). Add coverage
with two declarations producing the same warning category and verify both
suppression actions remain.

197-205: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve a trailing comma in an existing warning array.

If the source has @SuppressWarnings({"unchecked",}), Java accepts the trailing comma. Line 204 inserts another comma before }, which produces {"unchecked",, "rawtypes"}. Emit no leading comma when the array content already ends with one. Add a trailing-comma test case.

Proposed fix
         } else if (isArray) {
           val closeBrace = insideEnd - inside.reverse.indexOf('}') - 1
+          val hasTrailingComma =
+            value.substring(1, value.length() - 1).trim.endsWith(",")
           (
             new l.Range(
               text.indexToLspPosition(closeBrace),
               text.indexToLspPosition(closeBrace),
             ),
-            s""", "$warningName"""",
+            if (hasTrailingComma) s""""$warningName""""
+            else s""", "$warningName"""",
           )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala`
around lines 197 - 205, Update the array-handling branch in SuppressWarnings so
the generated insertion omits the leading comma when the existing array content
already ends with a comma, preventing duplicate commas while preserving normal
separator insertion otherwise. Add a test covering an existing trailing-comma
annotation such as `@SuppressWarnings`({"unchecked",}) and verify the resulting
warning array remains valid.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala`:
- Line 54: Update the action deduplication in SuppressWarnings so actions with
the same title but different target declarations or edits are preserved;
deduplicate using target or edit identity instead of getTitle(). Add coverage
with two declarations producing the same warning category and verify both
suppression actions remain.
- Around line 197-205: Update the array-handling branch in SuppressWarnings so
the generated insertion omits the leading comma when the existing array content
already ends with a comma, preventing duplicate commas while preserving normal
separator insertion otherwise. Add a test covering an existing trailing-comma
annotation such as `@SuppressWarnings`({"unchecked",}) and verify the resulting
warning array remains valid.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cf088187-bb6d-4459-b75d-6016420f7090

📥 Commits

Reviewing files that changed from the base of the PR and between b5dcdaf and 48c2076.

📒 Files selected for processing (7)
  • metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/SuppressWarnings.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala
  • mtags-java/src/main/scala/scala/meta/internal/jpc/JavaDiagnostics.scala
  • tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
🚧 Files skipped from review as they are similar to previous changes (4)
  • metals/src/main/scala/scala/meta/internal/metals/codeactions/CodeActionProvider.scala
  • tests/unit/src/test/scala/tests/codeactions/RemoveRedundantCastLspSuite.scala
  • tests/unit/src/test/scala/tests/codeactions/SuppressWarningsLspSuite.scala
  • metals/src/main/scala/scala/meta/internal/parsing/JavaTrees.scala

@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch 3 times, most recently from b9eb590 to 9d92ddb Compare August 13, 2026 15:22
@zielinsky
zielinsky force-pushed the metalsv2/supress-warnings branch from 9d92ddb to b10a7ca Compare August 13, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants